﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Simulation.LogicalGate
{
    /// <summary>
    /// This class represents a bit, which can define two states.
    /// </summary>
    [Serializable]
    public class Bit : IBit
    {
        /// <summary>
        /// The state of the bit.
        /// </summary>
        private bool state;

        /// <summary>
        /// Initializes a new instance of the <see cref="Bit"/> class.
        /// </summary>
        public Bit()
        {
            this.state = false;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Bit"/> class.
        /// </summary>
        /// <param name="state">The state of the bit.</param>
        public Bit(bool state)
        {
            this.state = state;
        }

        /// <summary>
        /// Gets or sets the state of the bit.
        /// </summary>
        /// <value>The state of the bit.</value>
        public bool State
        {
            get { return state; }
            set { state = value; }
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first bit.</param>
        /// <param name="b">The second bit.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Bit a, Bit b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return (a.State == b.State);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first bit.</param>
        /// <param name="b">The second bit.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Bit a, Bit b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current instance of a bit instance to another.
        /// </summary>
        /// <param name="compare">The bit instance to compare.</param>
        /// <returns>True if the bit state is even to the specified bit instance 
        /// otherwise, false</returns>
        public bool CompareTo(Bit compare)
        {
            if (compare == (Bit) null)
            {
                throw new ArgumentNullException("compare");
            }

            return this.State == compare.State;
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public Bit Copy()
        {
            return new Bit(this.state);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            if (this.state)
            {
                return "1";
            }

            return "0";
        }
    }
}